#include "ConsoleBox.h"
#include <conio.h>
ConsoleBox::ConsoleBox(int a_x, int a_y, int a_width, int a_height)
{
	m_x = a_x;
	m_y = a_y;
	m_width = a_width;
	m_height = a_height;
}

void ConsoleBox::AddString(char* a_buf)
{
	int length = strlen(a_buf);

	m_newMessage = true;

	// Push each individual character into the buffer
	for(int i = 0; i < length; i++)
		m_buf.push_back(a_buf[i]);
}

// CLear all of the data out of the buffer
void ConsoleBox::Clear()
{
	m_newMessage = false;
	m_buf.clear();
}


void ConsoleBox::Draw()
{
	int x = 0;
	int y = 0;

	// Only draw the console when there is a new message to draw.
	if(!m_newMessage)
		return;

	/// Draw the console borders first

	// Top line
	gotoXY(m_x+1, m_y);
	for(int i = 0; i < m_width; i++)
		_putch('-');

	for(int y = 1; y < m_height; y++)
	{
		// Left line
		gotoXY(m_x, m_y+y);
		_putch('|');
		
		// Blank area
		for(int i = 0; i < m_width; i++)
			putch(' ');

		// Right line
		_putch('|');
	}

	// Bottom line
	gotoXY(m_x+1, m_y+m_height);
	for(int i = 0; i < m_width; i++)
		_putch('-');

	// Now draw the text in the center
	for(int i = 0; i < m_buf.size(); i++)
	{
		// If there is a newline character, move one line down
		if(m_buf[i] == '\n')
		{
			x = 0;
			y++;
		}
		else // Draw a character
		{
			gotoXY(m_x + x + 1, m_y + y + 1);
			_putch(m_buf[i]);

			x++;
		}
		
		// If we've reached the end of this line, move to the next
		if(x >= m_width)
		{
			x = 0;
			y++;
		}

		
	}

	Clear();
}